import { HttpStatus } from "@nestjs/common";
import { number } from "joi";
import { PaginatedResponse } from "types/api-response.interface";

/**
 * 创建分页响应对象
 * @param options 响应选项
 * @returns 标准分页响应对象
 */
export const createPaginatedResponse = <T>(options: {
  code?: number;
  success?: boolean;
  message?: string;
  list?: T[];
  total?: number;
  page?: number;
  pageSize?: number;
  totalPages?: number;
}): PaginatedResponse<T> => {
  const {
    code = HttpStatus.OK,
    success = true,
    message = "查询成功",
    list = [],
    total = 0,
    page = 1,
    pageSize = 10,
    totalPages = 0,
  } = options;

  return {
    code,
    success,
    message,
    data: {
      list,
      total,
      page,
      pageSize,
      totalPages,
    },
  };
};

/**
 * 创建错误的分页响应
 * @param message 错误消息
 * @param page 当前页码
 * @param pageSize 每页数量
 * @param total 总数（可选）
 * @param totalPages 总页数（可选）
 * @returns 错误的分页响应对象
 */
export const createPaginatedErrorResponse = <T>(
  message: string,
  page: number = 1,
  pageSize: number = 10,
  total: number = 0,
  totalPages: number = 0,
): PaginatedResponse<T> => {
  return createPaginatedResponse<T>({
    code: HttpStatus.BAD_REQUEST,
    success: false,
    message,
    list: [],
    total,
    page,
    pageSize,
    totalPages,
  });
};

/**
 * 创建成功的分页响应
 * @param list 数据列表
 * @param total 总数
 * @param page 当前页码
 * @param pageSize 每页数量
 * @param message 成功消息
 * @returns 成功的分页响应对象
 */
export const createPaginatedSuccessResponse = <T>(
  list: T[],
  total: number,
  page: number,
  pageSize: number,
  message: string = "查询成功",
): PaginatedResponse<T> => {
  const totalPages = Math.ceil(total / pageSize);

  return createPaginatedResponse<T>({
    code: HttpStatus.OK,
    success: true,
    message,
    list,
    total: parseInt(total.toString()),
    page: parseInt(page.toString()),
    pageSize: parseInt(pageSize.toString()),
    totalPages: parseInt(totalPages.toString()),
  });
};
